﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.TcOperationProcessvalidator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.AssignDataFromTC;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands;

internal class TcOperationProcessvalidator : ITxValidator
{
  private CUiConsumeForm m_consumeForm;
  private CUiConsumeForm_ValidateSelectionDelegate m_validateSelection;

  public TcOperationProcessvalidator(
    CUiConsumeForm consumeForm,
    CUiConsumeForm_ValidateSelectionDelegate validateSelection)
  {
    this.m_consumeForm = consumeForm;
    this.m_validateSelection = validateSelection;
  }

  public ITxObject GetObject(string text)
  {
    ITxObject itxObject1 = (ITxObject) null;
    if (!string.IsNullOrEmpty(text))
    {
      TxObjectList objectsByName = TxApplication.ActiveDocument.GetObjectsByName(text);
      if (objectsByName != null && ((Collection<ITxObject>) objectsByName).Count > 0)
      {
        foreach (ITxObject itxObject2 in (Collection<ITxObject>) objectsByName)
        {
          if (this.IsValidObject(itxObject2, out string _))
          {
            itxObject1 = itxObject2;
            break;
          }
        }
      }
    }
    return itxObject1;
  }

  public string GetText(ITxObject obj)
  {
    string text = string.Empty;
    if (obj != null)
      text = obj.Name;
    return text;
  }

  public bool IsValidObject(ITxObject obj, out string errorMessage)
  {
    if (!this.m_validateSelection())
    {
      errorMessage = AssignDataFromTCResources.WARNING_SELECTION_TARGET;
      return false;
    }
    errorMessage = string.Empty;
    bool flag = obj is ITxProcessModelObject && (obj is ITxTcOperation || obj is TxTcProcess);
    using (new TxCallMessages.Collector((object) "TcOperationProcessvalidator.IsValidObject"))
    {
      if (flag && !obj.CanBeModified)
      {
        flag = false;
        errorMessage = string.Join("\r\n", TxCallMessages.GetMessages());
      }
      if (flag)
      {
        if (!this.m_consumeForm.AreConsumptionsValidAssignmentsForTarget(obj))
        {
          flag = false;
          errorMessage = string.Join("\r\n", TxCallMessages.GetMessages());
        }
      }
    }
    if (!flag && string.IsNullOrEmpty(errorMessage))
      errorMessage = AssignDataFromTCResources.WARNING_CHOOSE_TC_OPERATION_PROCESS;
    return flag;
  }

  public bool IsValidText(string text, out string errorMessage)
  {
    if (!this.m_validateSelection())
    {
      errorMessage = AssignDataFromTCResources.WARNING_SELECTION_TARGET;
      return false;
    }
    if (text == string.Empty || this.GetObject(text) != null)
    {
      errorMessage = string.Empty;
      return true;
    }
    errorMessage = AssignDataFromTCResources.WARNING_CHOOSE_TC_OPERATION_PROCESS;
    return false;
  }
}
